// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#ifndef MGCSTREAM_MCR
#define MGCSTREAM_MCR

typedef MgcObject* (*MgcFactoryFunction)(MgcStream&);

// insert in root class declaration
#define MgcDeclareRootStream \
    public: \
        static MgcTClassMap<MgcString,MgcFactoryFunction>* ms_pkFactory; \
        static int RegisterFactory (); \
        static MgcObject* Factory (MgcStream& rkStream); \
        virtual void Load (MgcStream& rkStream, MgcStream::Link* pkLink); \
        virtual void Link (MgcStream& rkStream, MgcStream::Link* pkLink); \
        virtual bool Register (MgcStream& rkStream); \
        virtual void Save (MgcStream& rkStream); \
        friend class _MgcObjectInitTerm

// insert in source file of root class of inheritance tree
#define MgcImplementRootStream \
    MgcTClassMap<MgcString,MgcFactoryFunction>* MgcObject::ms_pkFactory = 0; \
    class _MgcObjectInitTerm \
    { \
    public: \
        _MgcObjectInitTerm () \
        { \
            if ( !MgcObject::ms_pkFactory ) \
            { \
                MgcObject::ms_pkFactory = \
                    new MgcTClassMap<MgcString,MgcFactoryFunction>(128); \
            } \
            MgcObject::ms_pkFactory->SetAt("MgcObject",MgcObject::Factory); \
        } \
        ~_MgcObjectInitTerm () \
        { \
            delete MgcObject::ms_pkFactory; \
        } \
    }; \
    static _MgcObjectInitTerm _forceMgcObjectInitTerm

// insert in derived class declaration
#define MgcDeclareStream \
    public: \
        static int RegisterFactory (); \
        static MgcObject* Factory (MgcStream& rkStream); \
        virtual void Load (MgcStream& rkStream, MgcStream::Link* pkLink); \
        virtual void Link (MgcStream& rkStream, MgcStream::Link* pkLink); \
        virtual bool Register (MgcStream& rkStream); \
        virtual void Save (MgcStream& rkStream); \

// insert in source file of derived class of inheritance tree
#define MgcImplementStream(classname) \
    int classname::RegisterFactory () \
    { \
        if ( !ms_pkFactory ) \
        { \
            ms_pkFactory = \
                new MgcTClassMap<MgcString,MgcFactoryFunction>(128); \
        } \
        ms_pkFactory->SetAt(#classname,classname::Factory); \
        return 0; \
    }

// Insert in derived class header file.  Forces pre-main registration of
// factory whenever the header file is included in an application source file.
#define MgcRegisterStream(classname) \
    static int _##classname##registerFactory = classname::RegisterFactory ()

#endif
